use winapi::um::winuser::{
    GetForegroundWindow, GetKeyboardLayout, GetWindowThreadProcessId, PostMessageW,
    WM_INPUTLANGCHANGEREQUEST,
};
use winapi::shared::windef::HWND;
use winapi::shared::minwindef::LPARAM;

pub struct InputMethodManager {
    pub input_methods: Vec<i32>,
}

impl InputMethodManager {
    pub fn new(input_methods: Vec<i32>) -> Self {
        Self { input_methods }
    }

    pub fn get_current_input_method() -> i32 {
        unsafe {
            let hwnd: HWND = GetForegroundWindow();
            if !hwnd.is_null() {
                let thread_id = GetWindowThreadProcessId(hwnd, std::ptr::null_mut());
                let current_layout = GetKeyboardLayout(thread_id);
                (current_layout as u32 & 0x0000FFFF) as i32
            } else {
                0
            }
        }
    }

    pub fn switch_to_input_method(&mut self, locale: i32) {
        unsafe {
            let hwnd: HWND = GetForegroundWindow();
            if !hwnd.is_null() {
                let current_layout = locale as LPARAM;
                PostMessageW(hwnd, WM_INPUTLANGCHANGEREQUEST, 0, current_layout);
            }
        }
    }

    pub fn get_input_method_name(locale: i32) -> &'static str {
        match locale {
            0x1041 => "日文输入法",
            0x1033 => "英文输入法",
            0x2052 => "中文输入法",
            _ => "未知输入法",
        }
    }
} 